from odoo import api, fields, models, _


class ProductTemplate(models.Model):
    _inherit = 'product.template'

    s_required_iqc = fields.Boolean(default=False, string='需要IQC质检', compute='_compute_s_required_iqc', inverse='_set_s_required_iqc')

    @api.depends_context('company')
    @api.depends('product_variant_ids.s_required_iqc')
    def _compute_s_required_iqc(self):
        # Depends on force_company context because standard_price is company_dependent
        # on the product_product
        self._compute_template_field_from_variant_field('s_required_iqc')

    def _set_s_required_iqc(self):
        self._set_product_variant_field('s_required_iqc')

    @api.model_create_multi
    def create(self, vals_list):
        res = super(ProductTemplate, self).create(vals_list)
        for r in res:
            if r.categ_id.s_required_iqc:
                r.s_required_iqc = True
                r.product_variant_ids[0].s_required_iqc = True
        return res


class ProductProduct(models.Model):
    _inherit = 'product.product'

    s_required_iqc = fields.Boolean(default=False, string='需要IQC质检')
